const fs = require('fs');
const path = require('path');
let config = fs.readFileSync(path.join(__dirname,'./wdConfig.json'), 'utf8');
let res=null;
config = JSON.parse(config);
const TARGET_PATH=path.join(config.android.platform.buildPath,"jsb-default/frameworks/runtime-src/proj.android-studio");

init();
function init() {
    if(config.android.csjad.enable==true){
        AdFunc();
    }else {
        unityFunc();
    }
}
function AdFunc() {
    //穿山甲广告
    //1.修改WDNativePlatform文件
    let adScript=config.android.csjad.adScript;
    let adId=config.android.csjad.adId;
    res = fs.readFileSync(path.join(TARGET_PATH,"app/src/org/cocos2dx/javascript/Config.java"), 'utf8');
    res = res.replace("RewardCODE",adId.rewardVideoId);
    res = res.replace("FullScreenCODE",adId.fullScreenVideoId);
    res = res.replace("BANNERCODE",adId.bannerId);
    res = res.replace("SplashCODE",adId.splashId);
    res = res.replace("NativeExpressBannerCODE",adId.feedId);
    res = res.replace("APPID",config.android.platform.appId);
    res = res.replace("APPNAME",adId.appname);
    res = res.replace("ADID",adId.appid);
    res = res.replace("appsSecret",config.android.platform.secret);
    res = res.replace("InsertCODE",adId.insertId);
    saveFunc("app/src/org/cocos2dx/javascript/Config.java",res);
    //2.修改build.gradle文件
    res = fs.readFileSync(path.join(TARGET_PATH,"app/build.gradle"), 'utf8');
    res = res.replace("implementation project(':libcocos2dx')",adScript.dependencies);
    res = res.replace("java.srcDir \"src\"",adScript.sourceSets);
    saveFunc("app/build.gradle",res);
    res = fs.readFileSync(path.join(TARGET_PATH,"app/AndroidManifest.xml"), 'utf8');
    res = res.replace("<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",adScript.permission);
    res = res.replace("<application",adScript.application); 
    res = res.replace("</activity>",adScript.provider);
    saveFunc("app/AndroidManifest.xml",res);
    res = fs.readFileSync(path.join(TARGET_PATH,"gradle.properties"), 'utf8');
    res = res.replace("# fill in sign information for release mode",adScript.properties);
    res = res.replace("PROP_MIN_SDK_VERSION=16",adScript.sdkversion);
    saveFunc("gradle.properties",res);
    //4.复制相关文件
    let CSJAD_path= '././admodule/chuanshanjia/open_ad_sdk.aar'; 
    javascript_path =path.join(TARGET_PATH, 'app/libs/open_ad_sdk.aar');
    fs.copyFile(CSJAD_path,javascript_path,(err)=>{
        if(err) throw err;
        console.log("copy success open_ad_sdk.aar");
       
    }) 
}
function unityFunc() {
    let adId=config.android.csjad.adId;
     res = fs.readFileSync(path.join(TARGET_PATH,"app/src/org/cocos2dx/javascript/Config.java"), 'utf8');
    res = res.replace("RewardCODE",adId.rewardVideoId);
    res = res.replace("FullScreenCODE",adId.fullScreenVideoId);
    res = res.replace("BANNERCODE",adId.bannerId);
    res = res.replace("SplashCODE",adId.splashId);
    res = res.replace("NativeExpressBannerCODE",adId.feedId);
    res = res.replace("APPID",config.android.platform.appId);
    res = res.replace("APPNAME",adId.appname);
     res = res.replace("ADID",adId.appid);
     res = res.replace("appsSecret",config.android.platform.secret);
    saveFunc("app/src/org/cocos2dx/javascript/Config.java",res);
    res = fs.readFileSync(path.join(TARGET_PATH,"app/AndroidManifest.xml"), 'utf8');
    res = res.replace("com.wd.game",config.android.platform.packageName);
    res = res.replace("appKey",config.android.platform.appId);
    saveFunc("app/AndroidManifest.xml",res);
    res = fs.readFileSync(path.join(TARGET_PATH,"app/build.gradle"), 'utf8');
    res = res.replace("com.wd.game",config.android.platform.packageName);
    saveFunc("app/build.gradle",res);
}


function saveFunc(url,data) {
    fs.writeFileSync(path.join(TARGET_PATH, url), data, 'utf8');
}


